package com.rain.raindada.manager;

import com.rain.raindada.common.ErrorCode;
import com.rain.raindada.exception.BusinessException;
import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.*;
import io.reactivex.Flowable;
import org.bouncycastle.math.raw.Mod;
import org.intellij.lang.annotations.Flow;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class AiManager {

    @Resource
    private ClientV4 clientV4;

    //不稳定的随机数
    private static final float UNSTABLE_TEMPERATURE=0.99f;

    //稳定的随机数
    private static final float STABLE_TEMPERATURE=0.05f;


    /**
     * 通用请求
     * @param messages
     * @param stream
     * @param temperature
     * @return
     */
    public String doRequest(List<ChatMessage> messages,Boolean stream,Float temperature)
    {
        //构建请求
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                //使用模型是ChatGLM4，最新版本
                .model(Constants.ModelChatGLM4)
                //流式传输关闭
                .stream(stream)
                //随机性
                .temperature(temperature)
                //关闭异步传输，用同步传输
                .invokeMethod(Constants.invokeMethod)
                .messages(messages)
                .build();
        try
        {
            ModelApiResponse invokeModelApiResp = clientV4.invokeModelApi(chatCompletionRequest);
            return invokeModelApiResp.getData().getChoices().get(0).toString();
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,e.getMessage());
        }
    }


    /**
     * 通用请求(简化消息传递)
     * @param systemMessage
     * @param userMessage
     * @param stream
     * @param temperature
     * @return
     */

    public String doRequest(String systemMessage,String userMessage,Boolean stream,Float temperature)
    {
        List<ChatMessage> chatMessageList=new ArrayList<>();
        //系统消息
        ChatMessage systemChatMessage =new ChatMessage(ChatMessageRole.SYSTEM.value(), systemMessage);
        chatMessageList.add(systemChatMessage);

        //用户消息
        ChatMessage userChatMessage =new ChatMessage(ChatMessageRole.USER.value(), userMessage);
        chatMessageList.add(userChatMessage);
        return doRequest(chatMessageList,stream,temperature);
    }

    /**
     * 同步请求
     * @param systemMessage
     * @param userMessage
     * @param stream
     * @param temperature
     * @return
     */
    public String doSynRequest(String systemMessage,String userMessage,Boolean stream,Float temperature)
    {
        return doRequest(systemMessage,userMessage,Boolean.FALSE,temperature);
    }

    /**
     * 灵活地随机数生成同步请求
     * @param systemMessage
     * @param userMessage
     * @return
     */
    public String doSynUnstableRequest(String systemMessage,String userMessage)
    {
        return doRequest(systemMessage,userMessage,Boolean.FALSE,UNSTABLE_TEMPERATURE);
    }

    /**
     * 稳定地随机数生成同步请求
     * @param systemMessage
     * @param userMessage
     * @return
     */
    public String doSynStableRequest(String systemMessage,String userMessage)
    {
        return doRequest(systemMessage,userMessage,Boolean.FALSE,STABLE_TEMPERATURE);
    }

    /**
     * 通用流式请求
     * @param messages
     * @param temperature
     * @return
     */
    public Flowable<ModelData> doStreamRequest(List<ChatMessage> messages, Float temperature)
    {
        //构建请求
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                //使用模型是ChatGLM4，最新版本
                .model(Constants.ModelChatGLM4)
                //流式传输关闭
                .stream(Boolean.TRUE)
                //随机性
                .temperature(temperature)
                //关闭异步传输，用同步传输
                .invokeMethod(Constants.invokeMethod)
                .messages(messages)
                .build();
        try
        {
            ModelApiResponse invokeModelApiResp = clientV4.invokeModelApi(chatCompletionRequest);
            return invokeModelApiResp.getFlowable();
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,e.getMessage());
        }
    }

    public Flowable<ModelData> doStreamRequest(String systemMessage,String userMessage,Float temperature)
    {
        List<ChatMessage> chatMessageList=new ArrayList<>();
        ChatMessage systemChatMessage=new ChatMessage(ChatMessageRole.SYSTEM.value(), systemMessage);
        chatMessageList.add(systemChatMessage);
        ChatMessage userChatMessage=new ChatMessage(ChatMessageRole.USER.value(), systemMessage);
        chatMessageList.add(userChatMessage);
        return doStreamRequest(chatMessageList,temperature);
    }
}
